var hitValues = new Array(0, 0, 0, 0);

function applyVerifyCode() {
    document.getElementById("verifyCode").value = hitValues[0]+","+hitValues[1]+","+hitValues[2]+","+hitValues[3];
}

function hitVerifyImg(div, i) {
    var hitVal = hitValues[i];
    var backPos = div.style.backgroundPosition;
    var backPosArr = backPos.split(/\s+/);
    var backPosX = backPosArr[0];
    var backPosY = backPosArr[1].replace(/px/, '');
    var nextPoxY = (parseInt(backPosY) - 76) % 304;
    div.style.backgroundPosition = backPosX + " " + nextPoxY + "px";
    hitValues[i] = hitVal + 1;
}

function refreshVerifyImg() {
    hitValues = new Array(0, 0, 0, 0);
    $.ajax({
        type: "GET",
        url: "/dangdangOs/verifyCode",
        async: false,
        success: function(result){
            if(result.status == 200){
                var data = result.data;
                var verifyKey = data.verifyKey;
                document.getElementById("verifyKey").value = verifyKey;
                var imgUrl = "url(\""+data.verifyImgUrl+"\")";
                var div0 = document.getElementById("verifyHit0");
                var div1 = document.getElementById("verifyHit1");
                var div2 = document.getElementById("verifyHit2");
                var div3 = document.getElementById("verifyHit3");
                div0.style.backgroundImage = imgUrl;
                div0.style.backgroundPosition = "0px 0px";
                div1.style.backgroundImage = imgUrl;
                div1.style.backgroundPosition = "-76px 0px";
                div2.style.backgroundImage = imgUrl;
                div2.style.backgroundPosition = "-152px 0px";
                div3.style.backgroundImage = imgUrl;
                div3.style.backgroundPosition = "-228px 0px";
            } else {
                alert(result.message);
            }
        },
        error: function(xmlHttpRequest, textStatus, errorThrown){
            var errmsg = "请求失败: status=" + xmlHttpRequest.status + ", readyState=" + xmlHttpRequest.readyState
                + ", text=" + textStatus;
            alert(errmsg);
        }
    });
}
